package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.CardTypeConstant;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.RideCard;
import com.tbit.uqbike.client.service.*;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 骑行卡
 *
 * @author Leon
 * 2019年9月24日 下午5:28:46
 */
@Controller
@RequestMapping("/rideCard")
public class RideCardController {
    @Autowired
    private RideCardService rideCardService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private GeoService geoService;
    @Autowired
    private CardAccountBoundService cardAccountBoundService;
    @Autowired
    private AccountService accountService;

    /**
     * 分页查询骑行卡
     *
     * @param accountId   品牌id
     * @param lo          经度
     * @param la          纬度
     * @param mapType     地图类型
     * @param adAccountId 区域id
     * @param token       令牌
     * @param rowCount    每页大小
     * @param pageNO      页码数
     * @param session     会话
     * @param response    响应
     * @ignoreParams request
     */
    @RequestMapping("/queryPage")
    public void queryPage(Integer accountId, Double lo, Double la, Integer mapType, Integer adAccountId, String token, Integer rowCount, Integer pageNO,
                          HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /*默认值*/
            if (rowCount == null || rowCount < 1) {
                rowCount = 10;
            }
            if (pageNO == null || pageNO < 1) {
                pageNO = 1;
            }
            //Assert.notNull(adAccountId, "-10001", "品牌id不能为空");
            if (adAccountId == null) {
                Assert.notNull(lo, "-2001", "获取位置信息失败，请打开定位后重试");
                Assert.notNull(la, "-2002", "获取位置信息失败，请打开定位后重试");
                Integer agentId = geoService.getBelongAccount(accountId, lo, la, mapType);
                adAccountId = agentId;
            }
            List<RideCard> rideCards = new ArrayList<>();
            Integer count = 0;
            if (adAccountId != null) {
                rideCards = rideCardService.queryPage(adAccountId, pageNO, rowCount);
                /*获取总数*/
                count = rideCardService.queryCount(adAccountId);

            }
            List<String> name = new ArrayList<>();
            if (rideCards.size() > 0) {
                name.add("全车型");
                List<Integer> accountIds = cardAccountBoundService.getAccountId(adAccountId, accountId,
                        CardTypeConstant.TYPE_RIDECARD);
                String accountName;
                if (accountIds.isEmpty()) {
                    accountName = rideCards.get(0).getAccountName();
                } else {
                    List<String> names = accountService.getByAccountIds(accountIds);
                    accountName = names.stream().map(String::valueOf).collect(Collectors.joining(","));
                }
                for (RideCard r : rideCards) {
                    if (r.getFeeName() != null && !name.contains(r.getFeeName())) {
                        name.add(r.getFeeName());
                    }
                    if (r.getFeeName() == null) {
                        r.setFeeName("全车型");
                    }
                    r.setAccountName(accountName);
                }
            }
			/*List<RideCard> rideCards=rideCardService.queryPage(adAccountId,pageNO,rowCount);
			Integer count=rideCardService.queryCount(adAccountId);*/
            map.put("name", name);
            map.put("total", count);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, rideCards);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 根据id查询骑行卡信息
     *
     * @param token      令牌
     * @param rideCardId 骑行卡id
     * @param response   响应
     * @ignoreParams request
     */
    @RequestMapping("/getById")
    public void getById(String token, Integer rideCardId, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            Assert.notNull(rideCardId, "-120001", "骑行会员卡id不能为空");

            RideCard rideCard = rideCardService.getById(rideCardId);

            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, rideCard);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

}